﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Microsoft.TeamFoundation.Client;
using System.Net;

namespace WorkItemManager {
	public partial class UIDelete : Form {
		TeamFoundationServer _TFSServer = null;
		private List<WorkItemInfo> _SelectedWorkItems = null;

		public UIDelete(TeamFoundationServer server, List<WorkItemInfo> selectedWorkItems) {
			_SelectedWorkItems = selectedWorkItems;
			_TFSServer = server;
			InitializeComponent();
		}

		private void btnDelete_Click(object sender, EventArgs e) {
			XmlDocument document = new XmlDocument();
			XmlElement packageElement = document.CreateElement("Package");
			document.AppendChild(packageElement);

			foreach (WorkItemInfo wi in _SelectedWorkItems) {
				XmlElement childElement = document.CreateElement("DestroyWorkItem");
				childElement.SetAttribute("WorkItemID", wi.Id.ToString());
				packageElement.AppendChild(childElement);
			}

			string dbStamp;
			DataSet metadata;

			WorkItemProxy.ClientService cli = new WorkItemManager.WorkItemProxy.ClientService();
			cli.RequestHeaderValue = new WorkItemManager.WorkItemProxy.RequestHeader();
			cli.RequestHeaderValue.Id = "uuid:" + Guid.NewGuid().ToString();
			cli.Credentials = System.Net.CredentialCache.DefaultCredentials;
			cli.Url = _TFSServer.Uri.AbsoluteUri + "WorkItemTracking/v1.0/ClientService.asmx";
			cli.Update(packageElement, null, out dbStamp, out metadata);

			this.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e) {
			this.DialogResult = DialogResult.Cancel;
		}
	}
}
